package com.hunter.api.handler.exception;

import com.alibaba.fastjson.JSONObject;

import com.hunter.api.help.exception.BusinessException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class WholeExceptionHandler {
    //自定义异常返回对应编码
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public JSONObject handlerBusinessException(BusinessException e) {
        JSONObject json = new JSONObject();
        json.put("code",e.getCode());
        json.put("msg",e.toString());
        return json;
    }

    //其他异常报对应的信息
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JSONObject handlerSellException(Exception e) {
        JSONObject json = new JSONObject();
        json.put("code","500");
        json.put("msg",e.toString());
        return new JSONObject();
    }
}
